package cn.hxth.dubbo.customer.controller;

import cn.hxth.api.model.InstockRecord;
import cn.hxth.api.model.Product;
import cn.hxth.api.model.ProductBaseInfo;
import cn.hxth.api.model.ProductSpec;
import cn.hxth.api.query.InstockRecordQueryObject;
import cn.hxth.api.query.PageResult;
import cn.hxth.api.query.QueryObject;
import cn.hxth.api.service.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pages/instockRecord/")
public class InstockRecordController {
    @Resource(name = "myRedisTemplate")
    RedisTemplate<String,Object> redisTemplate ;
    @Resource
    private IInstockRecordService instockRecordService ;
    @Resource
    private IProductNameService productNameService ;
    @Resource
    private IProductSpecService productSpecService ;
    @Resource
    private IPlantService plantService ;
    @Resource
    private IProductService productService ;
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    @RequestMapping("insertInstockRecord")
    public boolean insertInstockRecord(InstockRecord instockRecord) {

        List<InstockRecord> instockRecords = instockRecord.getInstockRecords();
      //  System.out.println(instockRecords.size()) ;

        for(InstockRecord record:instockRecords){
            // 填充相关车间编号
            record.setPlant(this.plantService.getPlant(record.getPlant().getCid()));
            Long pid = this.productService.getPidBySid(record.getProduct().getSpec().getSid());
            record.getProduct().setPid(pid);
            // 设置入库时间
            record.setDate(new Date());
            Product product =  record.getProduct() ;
            JSONObject jsonObject = null;
            // 从redis中取出聚合数据用来填充入库信息
            String jsonStr = (String) redisTemplate.opsForValue().get("dim_product_" + product.getPid());
            System.out.println(jsonStr);
            // 如果数据为空
            if(jsonStr==null){
                //由商品业务层获取商品信息，并且把它变成json对象
                 jsonObject = JSONObject.parseObject(this.productService.getProduct(pid)) ;
                 // 从商品json串里取出商品规格编号
                Integer sid = jsonObject.getJSONObject("spec").getInteger("sid");
                // 获取规格信息
                ProductSpec spec = this.productSpecService.getProductSpec(sid) ;
                //System.out.println(JSONObject.toJSONString(spec));
                // 获取该规格对应的商品名称
                spec.setProductName(this.productNameService.getProductName(spec.getProductName().getNid()));
                // 将规格信息转为json串
                 JSONObject specJson = JSONObject.parseObject(JSONObject.toJSONString(spec)) ;
                 // 规格json串与商品json串合并
                jsonObject.put("spec",specJson) ;
                // 将此信息存入redis
                this.redisTemplate.opsForValue().set("dim_product_"+pid,jsonObject.toJSONString());




            }
            else{
                // 直接取得内存中的信息
                jsonObject = JSONObject.parseObject(jsonStr) ;
            }
            // 以下都是填充入库信息
            product.setFloor(jsonObject.getInteger("floor"));
            product.setSpec(jsonObject.getObject("spec", ProductSpec.class));
            product.setNote(jsonObject.getString("note"));
            product.setUnit(jsonObject.getString("unit"));
            product.setFin(jsonObject.getString("fin"));
            ProductBaseInfo productBaseInfo = jsonObject.getObject("product_base_info", ProductBaseInfo.class);
            record.setInventory(productBaseInfo.getInventory());
            record.setLin(productBaseInfo.getLin());
            record.setPrice(productBaseInfo.getPrice());
            record.setProductSpec(record.getProduct().getSpec().getTitle());
            record.setProductName(record.getProduct().getSpec().getProductName().getTitle());

        }
       /* for(InstockRecord record:instockRecords){
            // 为了防止读取不到对应商品读取可以读取缓存 我在控制层先把这两个值设进去
            record.getProduct().setSpec(this.productSpecService.getProductSpec(instockRecord.getProduct().getSpec().getSid(),instockRecord.getProduct().getSpec().getProductName().getNid()));
            record.getProduct().getSpec().setProductName(this.productNameService.getProductName(instockRecord.getProduct().getSpec().getProductName().getNid()));
        }*/
                 // 开始执行批量添加
        return this.instockRecordService.insertInstockRecord(instockRecords) ;
    }
    @RequestMapping("getInstockRecords")
    public PageResult getInstockRecords(InstockRecordQueryObject instockRecordQueryObject){
        return  this.instockRecordService.findAll(instockRecordQueryObject) ;
    }
    @RequestMapping("getAggrInfo")
    public  PageResult getAggrInfo(QueryObject queryObject){
        return  this.instockRecordService.findAggrInfo(queryObject) ;
    }
}
